package com.higgins.eventtracking.client.finance;

import java.util.Date;
import java.util.List;
import java.util.logging.Logger;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.DockLayoutPanel.Direction;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.higgins.eventtracking.client.AssetTrackingService;
import com.higgins.eventtracking.client.AssetTrackingServiceAsync;
import com.higgins.eventtracking.client.FinanceTrackingService;
import com.higgins.eventtracking.client.FinanceTrackingServiceAsync;
import com.higgins.eventtracking.client.SecurityAccessDeniedException;
import com.higgins.eventtracking.client.dto.TransactionDTO;
import com.higgins.eventtracking.client.finance.Charts.FinanceChartHandler;
import com.higgins.eventtracking.server.GenericDataStoreService;

public class Transactions extends VerticalPanel		{
	private static final Logger log = Logger.getLogger(Transactions.class.getName());
	private TransactionGenericColumns transactionColumns = null;
	private TransactionGenericTable transactionTable = null;
	private boolean firstRun = true;
	private final ListBox periodListBox = new ListBox();
	
	public Transactions()			{

	}
	public void update()	{
		if (firstRun)	{
			populateChartOffsetList();
			initialise(0);
		} else {
			initialise(periodListBox.getSelectedIndex()>=0?periodListBox.getSelectedIndex():0);
		}
	}
	
	public void refresh()	{
		
	}
	
	public void initialise(int selected)	{
		clear();
		add(periodListBox);
		firstRun = false;
		transactionColumns = new TransactionGenericColumns();
		transactionTable = new TransactionGenericTable(transactionColumns, selected);
		ScrollPanel scrollPanel = new ScrollPanel();
		scrollPanel.setSize("900px", "400px");
		scrollPanel.add(transactionTable);
		add(scrollPanel);
		load();
	}
	private void load()	{
		try {
			/*
			financeTrackingService.retrieveTransactions(periodListBox.getSelectedIndex(), true, new AsyncCallback<List<TransactionDTO>>()	{
					@Override
					public void onFailure(Throwable caught) {
						log.info("Transactions: " + caught.toString());
					}

					@Override
					public void onSuccess(List<TransactionDTO> result) {
						transactionTable.update(result);
					}
				});
				*/
		} catch (IllegalArgumentException e) {
			log.info("Transactions: " + e.toString());
		} 
	}
	private void populateChartOffsetList()	{
		periodListBox.addItem("1");
		periodListBox.addItem("2");
		periodListBox.addItem("3");
		periodListBox.addItem("4");
		periodListBox.addItem("5");
		periodListBox.addItem("6");
		periodListBox.addItem("7");
		periodListBox.addItem("8");
		periodListBox.addItem("9");
		periodListBox.addItem("10");
		periodListBox.addItem("11");
		periodListBox.addItem("12");
		periodListBox.addItem("13");
		periodListBox.addItem("14");
		periodListBox.addItem("15");
		periodListBox.addItem("16");
		periodListBox.addItem("17");
		periodListBox.addItem("18");
		periodListBox.addItem("19");
		periodListBox.addItem("20");
		periodListBox.addItem("21");
		periodListBox.addItem("22");
		periodListBox.addItem("23");
		periodListBox.addItem("24");
		periodListBox.addItem("25");
		periodListBox.addItem("26");
		periodListBox.setSelectedIndex(0);
		periodListBox.addChangeHandler(new TransactionTableHandler());		
	}		
	class TransactionTableHandler implements ChangeHandler {
		public void onChange(ChangeEvent event) {
//			load();
			initialise(periodListBox.getSelectedIndex());
		}
	}	

}
